<template>
  <view class="main">
    <Notices />
    <view class="nva">
      <view class="nva-list" v-for="(item, index) in nvaList" :key="index" @click="handleNav(item)">
        <view class="img">
          <image class="image" :src="item.icon" mode="aspectFit" />
        </view>
        <view class="text">{{item.text}}</view>
      </view>
    </view>
    <view class="products">
      <view class="products-content">
        <template v-for="(item, index) in productList" :key="index">
          <Product :product="item" />
        </template>
      </view>
    </view>
    <TabBar></TabBar>
  </view>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { onLoad, onReachBottom, onPullDownRefresh, onShareAppMessage } from '@dcloudio/uni-app'
import Notices from '../../components/notices/index.vue' ;
import Product from '@/components/product/Product.vue' ;
import nva_new_product from '@/static/svg/nva_new_product.svg'
import nva_order from '@/static/svg/nva_order.svg'
import nva_hot from '@/static/svg/nva_hot.svg'
import nva_point from '@/static/svg/nva_point.svg'

import { getIndexProductList, iconAllApi } from '@/api/product'
// import 
const nvaList = ref([

  {
    icon: nva_point,
    text: '积分商城',
    path: '/pages/point/index',
    type: '',
  },
  {
    icon: nva_order,
    text: '返现奖励',
    path: '/subpkgs/mine/withdraw/index'
  },
])
const queryData = reactive({
  page: 1,
  size: 10,
  type: 1,
  isLast: false
})
const productList = ref([])
onLoad(()=> {
  init()
  getIconAll()
})
onShareAppMessage(()=> {
  return { }
})
onPullDownRefresh( async ()=> {
  await init().finally(() => {
    uni.stopPullDownRefresh();
  });
})
onReachBottom(()=> {
  queryData.page += 1;
  getProduct()
})
async function init() {
  queryData.page = 1;
  queryData.isLast = false
  productList.value = []
  await getProduct()
}
async function getProduct(){
  if(queryData.isLast) {
    return 
  }
  const res = await getIndexProductList({...queryData})
  productList.value.push(...res.items)
  if(productList.value.length === res.total) {
    queryData.isLast = true
  } else {
    queryData.isLast = false
  }
  console.log('res', res)
}
async function getIconAll(){
  const res = await iconAllApi()
  const hotIcon = res.find((item)=> item.iconName === '热卖')
  const newIcon =res.find((item)=> item.iconName === '新品')
  if(hotIcon) {
      nvaList.value.unshift({
        icon: nva_hot,
        text: '热卖专区',
        path: '/pages/product/index?iconId=' + hotIcon.id
      })
  }
  if(newIcon) {
      nvaList.value.unshift({
        icon: nva_new_product,
        text: '新品专区',
        path: '/pages/product/index?iconId=' + newIcon.id
      })
  }
}
function handleNav(item){
  if(item.path) {
    if(item.type === 'tabbar') {
      uni.switchTab({
        url: item.path
      })
    } else {
      uni.navigateTo({
        url: item.path
      })
    }

  }
}
</script>

<style lang='scss' scoped>
.main{
  width: 100vw;
  min-height: 100vh;
  background: #F4F6F8;
  overflow-x: hidden;
  .nva{
    display: flex;
    margin-top: 20rpx;
    background: #fff;
    padding: 32rpx 0;
    .nva-list{
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      .img{
        width: 90rpx;
        height: 90rpx;
        border-radius: 24rpx 24rpx 24rpx 24rpx;
        border: 2rpx solid rgba(40,55,74,0.02);
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 8rpx;
        .image{
          width: 90rpx;
          height: 90rpx;
        }
      }
      .text{
        font-weight: 400;
        font-size: 24rpx;
        color: #333333
      }
    }
  }
  .products{
    background: #fff;
    padding: 32rpx;
    margin-top: 20rpx;
    .tit{
      font-weight: 500;
      font-size: 28rpx;
      color: #333333;
      line-height: 36rpx;
      margin-bottom: 20rpx;
    }
    .products-content{
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 24rpx;
    }
  }
}
</style>
